package com.ws.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ws.eduservice.entity.EduChapter;
import com.ws.eduservice.entity.EduVideo;
import com.ws.eduservice.entity.chapter.ChapterVo;
import com.ws.eduservice.entity.chapter.VideoVo;
import com.ws.eduservice.mapper.EduChapterMapper;
import com.ws.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ws.eduservice.service.EduVideoService;
import com.ws.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-12-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getAddChaperList(String couserId) {

        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", couserId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);

        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper.eq("course_id", couserId);
        List<EduVideo> eduVideoList = videoService.list(wrapper2);

        List<ChapterVo> chapterVoList = new ArrayList<>();
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            chapterVoList.add(chapterVo);

            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {
                if(eduChapter.getId().equals(eduVideo.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);

        }



        return chapterVoList;
    }
//章节中有小节不删除
    @Override
    public Boolean deleteById(String id) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        int count = videoService.count(wrapper);
        if (count > 0) {
            throw new MyException(20001,"不能删除");
        }
        int i = baseMapper.deleteById(id);
        return i>0;
    }

    @Override
    public void deleteByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        baseMapper.delete(queryWrapper);
    }

//    @Override
//    public String addChapter(String couserId) {
//        EduChapter eduChapter = new EduChba).setCourseId(couserId);
//        int insert = baseMapper.insert(eduChapter);
//        if (insert == 0) {
//            throw new MyException(20001,"添加章节失败");
//        }
//        return eduChapter.getId();
//    }
}
